package jrobutt;

public class Vector2
{
    public float x;
    public float y;
    
    public static final Vector2 ZERO =   new Vector2(0.0f, 0.0f);
    public static final Vector2 ONE =    new Vector2(1.0f, 1.0f);
    public static final Vector2 UNIT_X = new Vector2(1.0f, 0.0f);
    public static final Vector2 UNIT_Y = new Vector2(0.0f, 1.0f);
    
    public Vector2 (float x, float y)
    {
        this.x = x;
        this.y = y;
    }
    public Vector2 ()
    {
        this(0.0f, 0.0f);
    }
    public Vector2 (Vector2 v)
    {
        this(v.x, v.y);
    }
    
    public static Vector2 from_length_and_angle (float length, float angle)
    {
        return new Vector2((float)(length * Math.cos(angle)), (float)(length * Math.sin(angle)));
    }
    
    public Vector2 cloned ()
    {
        return new Vector2(this);
    }
    
    public float getLength ()
    {
        return GameMath.point_distance(0.0f, 0.0f, x, y);
    }
    public float getAngle ()
    {
        return GameMath.point_direction(0.0f, 0.0f, x, y);
    }
    
    public void setAngle (float angle)
    {
        float length = this.getLength();
        this.x = (float)(length * Math.cos(angle));
        this.y = (float)(length * Math.sin(angle));
    }
    
    public void set (Vector2 v)
    {
        x = v.x;
        y = v.y;
    }
    
    public void add (Vector2 v)
    {
        this.x += v.x;
        this.y += v.y;
    }
    public void subtract (Vector2 v)
    {
        this.x -= v.x;
        this.y -= v.y;
    }
    public void scale (Vector2 v)
    {
        this.x *= v.x;
        this.y *= v.y;
    }
    public void divide (Vector2 v)
    {
        this.x /= v.x;
        this.y /= v.y;
    }
    
    public void scale (float f)
    {
        this.x *= f;
        this.y *= f;
    }
    public void divide (float f)
    {
        this.x /= f;
        this.y /= f;
    }
    
    public void normalize ()
    {
        float length = getLength();
        if (length == 0.0f)
        {
            x = 1.0f;
            y = 0.0f;
        }
        else
        {
            x /= length;
            y /= length;
        }
    }
    
    public void rotate (float dx, float dy)
    {
        float temp_x = this.x;
        this.x = this.x * dx - this.y * dy;
        this.y = this.y * dx + temp_x * dy;
    }
    
    public boolean equals (Object o)
    {
        if (o instanceof Vector2)
        {
            return equals((Vector2)o);
        }
        else
        {
            return false;
        }
    }
    public boolean equals (Vector2 v)
    {
        return x == v.x && y == v.y;
    }
}
